package cc.openkit.admin.controller.adv.list;

import cc.openkit.admin.model.AdvBox;
import cc.openkit.admin.model.AdvList;
import cc.openkit.admin.model.AdvType;
import cc.openkit.admin.model.GGroupLimit;
import cc.openkit.admin.service.adv.box.AdvBoxService;
import cc.openkit.admin.service.adv.list.AdvListService;
import cc.openkit.admin.service.adv.type.AdvTypeService;
import cc.openkit.admin.service.g.GGroupLimitService;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.common.KitUtil;
import cc.openkit.kitIsNull.KitVerification;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告列表管理
 *
 * @author bigben
 * @date 2018.4.2
 */
@Controller
@Scope("prototype")
@RequestMapping("/adv_list")
public class AdvListController {
    private Logger log = Logger.getLogger(AdvListController.class);

    @Resource
    private AdvListService advListService;
    @Resource
    private AdvBoxService advBoxService;
    @Resource
    private AdvTypeService advTypeService;
    @Resource
    private GGroupLimitService gGroupLimitService;

    /**
     * 去添加页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/goAdd", method = RequestMethod.GET)
    public Object goAdd(HttpServletRequest request){
        log.info("推荐位列表 》 添加 》 跳转");
        // 获取推荐位列表
        List<AdvType> advTypeList = advTypeService.queryAll();
        ModelAndView mv = new ModelAndView();

        if(advTypeList.size()==0){
            // 说明没有推荐位类型，提示先添加推荐位类型
            mv.setViewName("/admin/adv_ty/add");
        }

        String id = request.getParameter("id");
        mv.addObject("id",id); // 返回广告位ID
        mv.addObject("kitList",advTypeList);

        mv.setViewName("/admin/adv_list/add");
        return mv;
    }

    /**
     * 添加提交
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(HttpServletRequest request) throws Exception{
        log.info("广告列表 》 添加 》 保存");

        // 取值
        String boxId = request.getParameter("boxId");
        String advTypeId = request.getParameter("advTypeId");
        String title = request.getParameter("title");
        String no = request.getParameter("no");
        String imgurl = request.getParameter("imgurl");
        String subhead = request.getParameter("subhead");
        String explain = request.getParameter("explain");
        String oldPrice = request.getParameter("oldPrice");
        String newPrice = request.getParameter("newPrice");
        String time = request.getParameter("time");
        String informationId = request.getParameter("informationId");
        String editor = request.getParameter("content1");

        if(boxId==null||"".equals(boxId)){
            return JSONObject.toJSON(KitUtil.returnMap("101","推荐位ID不能为空"));
        }
        if(advTypeId==null||"".equals(advTypeId)){
            return JSONObject.toJSON(KitUtil.returnMap("101","广告类型必须填写"));
        }

        // 封装对象
        AdvList advList = new AdvList();
        advList.setId(KitUtil.uuid());
        advList.setBoxId(Integer.valueOf(boxId));
        advList.setAdvTypeId(advTypeId);
        advList.setTitle(title);
        advList.setSubhead(subhead);
        advList.setAlNumber(KitUtil.feikong(no)?Integer.valueOf(no):50);
        advList.setImg(imgurl);
        advList.setAlExplain(KitUtil.feikong(explain)?explain:null);
        advList.setOldPrice(KitUtil.feikong(oldPrice)?Double.valueOf(oldPrice):null);
        advList.setNewPrice(KitUtil.feikong(newPrice)?Double.valueOf(newPrice):null);
        advList.setInformationId(informationId);
        if(!" - ".equals(time)){
            String[] times = time.split(" - ");
            advList.setStaticTime(KitUtil.stringToData(times[0],"yyyy-MM-dd HH:mm:ss"));
            advList.setEndTime(KitUtil.stringToData(times[1],"yyyy-MM-dd HH:mm:ss"));
            advList.setTimeSetting(1);
        }else{
            advList.setTimeSetting(2);
        }
        advList.setEditor(editor);
        advList.setAlTime(new Date());

        // 保存并返回
        return JSONObject.toJSON(advListService.saveSelect(advList)==1 ? KitUtil.returnMap("200",StaticFinalVar.ADD_OK) : KitUtil.returnMap("101",StaticFinalVar.ADD_ERR));
    }

    /**
     * 去首次打开APP广告列表页面
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView index(HttpServletRequest request){
        log.info("痰喘到对应的广告位的广告列表，值显示标题可以产看详情可以修改");

        // 权限验证
        ModelAndView mv = new ModelAndView();
        String id = request.getParameter("id");

        // 取值
        mv.addObject("id",id);
        mv.setViewName("/admin/adv_list/list");
        return mv;
    }
    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "/getAllJson", method = RequestMethod.POST)
    @ResponseBody
    public Object getAllJson(HttpServletRequest request){
        log.info("获取所有的广告列表");

        Map<String,Object> map = new HashMap<String,Object>();

        String page = request.getParameter("page");// 获得页数
        String limit = request.getParameter("limit");// 获得每页显示条数
        String search = request.getParameter("search");// 获取搜索条件

        // 封装数据
        AdvBox advBox = new AdvBox();
        if(KitUtil.feikong(search)){
            advBox.setName(search);
        }

        // 获取推荐位的ID
        String id = request.getParameter("id");
        AdvList advList = new AdvList();
        advList.setBoxId(Integer.valueOf(id));

        // 分页查询
        List<AdvList> list = advListService.queryPageListByWhere(advList, Integer.valueOf(page), Integer.valueOf(limit));
        int size = advListService.queryCount(advList);

        // 返回数据
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",list);
        return JSONObject.toJSON(map);

    }

    /**
     * 删除
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Object del(HttpServletRequest request) throws Exception{
        log.info("删除广告列表状态");
        String id = request.getParameter("id");
        int i = advListService.deleteByUUId(id);
        return i==1?KitUtil.returnMap("200",StaticFinalVar.DEL_OK):KitUtil.returnMap("101",StaticFinalVar.DEL_ERR);
    }


    /**
     * 修改前的查询
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/goUpdate", method = RequestMethod.GET)
    public ModelAndView goUpdate(HttpServletRequest request) throws Exception{
        String id = request.getParameter("id");
        AdvList model = advListService.queryByUUID(id);

        ModelAndView mv = new ModelAndView();

        List<AdvType> advTypeList = advTypeService.queryAll();

        if(advTypeList.size()==0){
            // 说明没有推荐位类型，提示先添加推荐位类型
            mv.setViewName("/admin/adv_ty/add");
            return mv;
        }

        AdvType advType = advTypeService.queryByUUID(model.getAdvTypeId());

        mv.addObject("kitList",advTypeList);
        mv.setViewName("/admin/adv_list/update");
        mv.addObject("kitModel",model);
        mv.addObject("advType",advType);
        return mv;
    }

    /**
     * 修改
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request) throws Exception{
        log.info("广告列表 》 修改 》 保存");

        // 取值
        String id = request.getParameter("id");
        String advTypeId = request.getParameter("advTypeId");
        String title = request.getParameter("title");
        String no = request.getParameter("no");
        String imgurl = request.getParameter("imgurl");
        String subhead = request.getParameter("subhead");
        String explain = request.getParameter("explain");
        String oldPrice = request.getParameter("oldPrice");
        String newPrice = request.getParameter("newPrice");
        String time = request.getParameter("time");
        String informationId = request.getParameter("informationId");
        String editor = request.getParameter("content1");

        if(advTypeId==null||"".equals(advTypeId)){
            return JSONObject.toJSON(KitUtil.returnMap("101","广告类型必须填写"));
        }

        // 封装对象
        AdvList advList = new AdvList();
        advList.setId(id);
        advList.setAdvTypeId(advTypeId);
        advList.setTitle(title);
        advList.setSubhead(subhead);
        advList.setAlNumber(KitUtil.feikong(no)?Integer.valueOf(no):50);
        advList.setImg(imgurl);
        advList.setAlExplain(KitUtil.feikong(explain)?explain:null);
        advList.setOldPrice(KitUtil.feikong(oldPrice)?Double.valueOf(oldPrice):null);
        advList.setNewPrice(KitUtil.feikong(newPrice)?Double.valueOf(newPrice):null);
        advList.setInformationId(informationId);
        if(!" - ".equals(time)){
            String[] times = time.split(" - ");
            advList.setStaticTime(KitUtil.stringToData(times[0],"yyyy-MM-dd HH:mm:ss"));
            advList.setEndTime(KitUtil.stringToData(times[1],"yyyy-MM-dd HH:mm:ss"));
            advList.setTimeSetting(1);
        }else{
            advList.setTimeSetting(2);
        }
        advList.setEditor(editor);
        advList.setUpdateTime(new Date());


        // 修改保存并返回参数
        return JSONObject.toJSON(advListService.updateSelective(advList)==1 ? KitUtil.returnMap("200",StaticFinalVar.UPDATE_OK) : KitUtil.returnMap("101",StaticFinalVar.UPDATE_ERR));
    }
}
